/* Copyright (c) 2008, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

/* parse the command line */

/* command line arguments are in the form CMD_ARG = [' '*]NAME[' '*]['='[' '*]VALUE[' '*]]
The whole command line is CMD_LINE = [CMD_ARG][(','CMD_ARG)*] */

#include "cmdline.h"
#include "stdlib.h"
#include "console.h"

void interpret_cmd_arg(const char *name, const char *arg);
void _strip_whitespace(char **s);

char *kernel_name;

void interpret_cmd_arg(const char *name, const char *arg)
{
#ifdef _DEBUG
	_puts("    Interpreting command line arg: ", 0);
	_puts(name, 0);
	_puts(", val: ", 0);
	puts(arg);
#endif
	if(!strcmp("kernel", name))
		kernel_name = (char *)arg;
}

void parse_cmd_line(char *s)
{
	int i = 0;

#ifdef _DEBUG
	_puts("  Interpreting command line: ", 0);
	puts(s);
#endif

	while(*s == ' ') {
		s++;
	}
	if(*s == '\0')
		return;

	while(1) {
		char term_char;
		char *cur_str = &s[i];
		while((s[i] != '\0') && (s[i] != ','))
			i++;
		term_char = s[i];
		if(term_char == ',') {
			s[i] = '\0';
			i++;
		}
		parse_cmd_arg(cur_str);
		if(term_char == '\0')
			return;
	}
}

void parse_cmd_arg(char *cur_str)
{
	char *arg_name, *arg_val;

	arg_name = cur_str;
	
	while((*cur_str != '\0') && (*cur_str != '='))
		cur_str++;
	
	if(*cur_str == '\0')
		arg_val = NULL;
	else {
		*cur_str = '\0';
		arg_val = cur_str + 1;
	}

	_strip_whitespace(&arg_name);
	if(arg_val != NULL)
		_strip_whitespace(&arg_val);

	interpret_cmd_arg(arg_name, arg_val);
}

void _strip_whitespace(char **s)
{
	/* strip leading and trailing whitespace from a string */
	char *trailing;
	char *leading = *s;
	while(*leading == ' ')
		leading++;
	*s = leading;

	/* find the last character of the string */
	trailing = *s;
	while(*trailing != '\0')
		trailing++;
	trailing--;

	while((*trailing == ' ') && (trailing >= *s))
		trailing--;
	trailing++;
	*trailing = '\0';
}
